<%@ page import="br.com.rosena.control.ControllerRosena" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="br.com.rosena.model.bean.persistente.*" %>

<%
    String nomeClasseRR = lstAtributo[i].getTipo().replace(".xml", "");
    Persistente objPersistenteRR = new Persistente(nomeClasseRR);
    Classe objClasseRR = objPersistenteRR.getClasse();
    Atributo lstAtributoRR[] = objClasseRR.getArrayAtributos();
    Metodo lstMetodoRR[] = objClasseRR.getArrayComando();
    br.com.rosena.util.Util.bubbleSortMetodo(lstMetodoRR);
    br.com.rosena.util.Util.bubbleSortAtributo(lstAtributoRR);
%>
<script type="text/javascript">  var obj<%=nomeClasseRR%> = new Persistente('<%=nomeClasseRR%>'); </script>
<fieldset>
    <legend><%=lstAtributo[i].getNomeUsual().replace("_"," ")%></legend>
    <table class="Tabela01" cellpadding="0" cellspacing="0" >
        <tr>
            <td width="12.5%"></td>
            <td width="12.5%"></td>
            <td width="12.5%"></td>
            <td width="12.5%"></td>
            <td width="12.5%"></td>
            <td width="12.5%"></td>
            <td width="12.5%"></td>
            <td width="12.5%"></td>
        </tr>
        <tr><td colspan="8"><br></td></tr>
        <% colspan = 0;
            entrar = false;
            ControllerRosena controleRR = new ControllerRosena();
            for (int ii = 0; ii < lstAtributoRR.length; ii++) {
                Atributo atributoR = lstAtributoRR[ii];
                String tipoAtributoRR = atributoR.getTipo();
                if (atributoR.getNome().equals(lstAtributo[i].getValorPadraoTela())) {
                    continue;
                }
                colspan += atributoR.getTamanhoLargura();
                if (colspan > 8 || i == 0 || entrar) {%>
                    <tr>
                    <%entrar=false;%>
                <%}%>
                <%if(tipoAtributoRR.contains(Atributo.XML)){%>
                    <%if(!atributoR.getConsultaDoAtributo().equals(Atributo.XML_LIST) ){%>
                        <td colspan="<%= br.com.rosena.util.Util.formatarNumero(atributoR.getTamanhoLargura(),"0")%>">
                            <%=atributoR.isObrigatorio()  && !atributoR.isSomenteLeitura() ? "<font color=\"red\" >"+atributoR.getNomeUsual().replace("_"," ")+"</font>": atributoR.getNomeUsual().replace("_"," ")%>:
                            <img src="imagens/carregar.png" alt="" style="cursor:hand" onclick="abrirPagina('<%=tipoAtributoRR.replace(".xml","")%>',document.getElementById('<%=atributoR.getNome()%><%=nomeClasseRR%>').value)">
                            <img src="imagens/lupinha.png" alt="" style="cursor:hand" onclick="abrirConsulta('<%=tipoAtributoRR.replace(".xml","")%>','<%=atributoR.getNome()%><%=nomeClasseRR%>')"><br>
                            <select name="<%=atributoR.getNome()%><%=nomeClasseRR%>" id="<%=atributoR.getNome()%><%=nomeClasseRR%>" onchange="obj<%=nomeClasseRR%>.setPersistente('<%=atributoR.getNome()%>',document.getElementById('<%=atributoR.getNome()%><%=nomeClasseRR%>').value);" class="Combo01" <%=atributoR.isSomenteLeitura() ? "disabled=\"disable\"" : ""%>>
                                <option value="0">Selecione uma opcao</option>
                                <% Persistente objPersistenteInterno = getPersistente(objPersistenteRR, atributoR.getNome().trim().toUpperCase());
                                    String consultaAtributo = atributoR.getConsultaDoAtributo() != null ? atributoR.getConsultaDoAtributo() : "";
                                    if (!consultaAtributo.equals("")) {
                                        Consulta consulta = objPersistenteInterno.getClasse().getConsulta(consultaAtributo);
                                        if (consulta != null) {
                                            ResultSet crsAtributo = controleRR.execultarQueryReturResultSet(consulta.getSql());
                                            while (crsAtributo.next()) {%>
                                                <%if( objPersistenteRR.getPersistente(atributoR.getNome()).getId()  == crsAtributo.getLong("id_objeto") ){%>
                                                    <option  selected="selected" value="<%=crsAtributo.getString("id_objeto")%>"><%=crsAtributo.getString("descricao_objeto")%></option>
                                                <%}else{%>
                                                    <option value="<%=crsAtributo.getString("id_objeto")%>"><%=crsAtributo.getString("descricao_objeto")%></option>
                                                <%}%>
                                            <%}%>
                                     <%}%>
                                 <%}%>
                            </select>
                        </td>
                    <%}%>
                <%}else if(tipoAtributoRR.equals(Atributo.String)){
                        String consultaAtributo = atributoR.getConsultaDoAtributo() != null ? atributoR.getConsultaDoAtributo() : "";%>
                        <%if( consultaAtributo.endsWith(Atributo.XML_OPS) ){%>
                            <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributoR.getTamanhoLargura(),"0")%>">
                                     <%=atributoR.isObrigatorio()  && !atributoR.isSomenteLeitura() ? "<font color=\"red\" >"+atributoR.getNomeUsual().replace("_"," ")+"</font>": atributoR.getNomeUsual().replace("_"," ")%>:<br>
                                    <select name="<%=atributoR.getNome()%><%=nomeClasseRR%>" onchange="obj<%=nomeClasseRR%>.setString('<%=atributoR.getNome()%>',document.getElementById('<%=atributoR.getNome()%><%=nomeClasseRR%>').value);" class="Combo01" <%=atributoR.isSomenteLeitura() ? "disabled=\"disable\"" : ""%>>
                                        <%String valorCombo[] = atributoR.getOpcaoTela().split(";");
                                          for(int h=0;h<valorCombo.length;h++){%>
                                            <option value="<%=valorCombo[h]%>"><%=valorCombo[h]%></option>
                                          <%}%>
                                    </select>
                             </td>
                        <%}else if(consultaAtributo.endsWith(Atributo.XML_IMAGEM) ){%>
                            <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributoR.getTamanhoLargura(),"0")%>">
                                <fieldset>
                                    <legend><%=atributoR.getNomeUsual()%></legend>
                                    <center>
                                        <input type="button" value="Carregar" onclick="uploadArquivo<%=nomeClasse%>();" >
                                        <input type="button" value="Abrir"  onclick="window.open( document.getElementById('img<%=nomeClasse%>').src, '_ARQUIVO_<%=nomeClasse%>' );" >
                                    </center>
                                    <script >
                                        function uploadArquivo<%=nomeClasseRR%>(){
                                            try{
                                               obj<%=nomeClasseRR%>.setPersistente('<%=lstAtributo[i].getValorPadraoTela()%>',obj<%=nomeClasse%>.getString("id") );//inversa
                                                var pagina = WEBSITE+"upload/uploadArquivoMultiplos.jsp?nomeClasse=<%=nomeClasseRR%>&toStringJson="+obj<%=nomeClasseRR%>.getClasse().getDados().toJsonRosenaString();
                                                var width = 150;
                                                var height = 250;
                                                var left = 99;
                                                var top = 99;
                                                window.open(pagina,'_UPLOAD');
                                            }catch(ex){
                                                alert(ex.message);
                                            }
                                        }
                                    </script>
                                    <%=atributoR.isObrigatorio() && !atributoR.isSomenteLeitura() ? "<font color=\"red\" >"+atributoR.getNomeUsual().replace("_"," ")+"</font>": atributoR.getNomeUsual().replace("_"," ")%>:<br>
                                    <input id="<%=atributoR.getNome()%><%=nomeClasseRR%>" name="<%=atributoR.getNome()%><%=nomeClasseRR%>" onchange="obj<%=nomeClasseRR%>.setString('<%=atributoR.getNome()%>',document.getElementById('<%=atributoR.getNome()%><%=nomeClasseRR%>').value);" type="file" disabled="disabled"  class="<%=atributoR.isSomenteLeitura() ? "Input04" : "Input04" %>" value="<%=objPersistenteRR.getString(atributoR.getNome())%>">
                                </fieldset>
                            </td>
                        <%} else if(consultaAtributo.endsWith(Atributo.XML_ARQUIVO)){%>
                            <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributo.getTamanhoLargura(),"0")%>">
                                <fieldset>
                                    <legend><%=atributo.getNomeUsual()%></legend>
                                    <center><iframe src="pagina_arquivos.jsp" id="img<%=nomeClasse%>" width="100%" scrolling="auto" height="350px"></iframe></center>
                                    <center>
                                        <input type="button" value="Carregar" onclick="uploadArquivo<%=nomeClasse%>();" >
                                        <input type="button" value="Abrir"  onclick="window.open( document.getElementById('img<%=nomeClasse%>').src, '_ARQUIVO_<%=nomeClasse%>' );" >
                                    </center>
                                    <script type="text/javascript">
                                         function uploadArquivo<%=nomeClasseRR%>(){
                                            try{
                                               obj<%=nomeClasseRR%>.setPersistente('<%=lstAtributo[i].getValorPadraoTela()%>',obj<%=nomeClasse%>.getString("id") );//inversa
                                                var pagina = WEBSITE+"upload/uploadArquivoMultiplos.jsp?nomeClasse=<%=nomeClasseRR%>&toStringJson="+obj<%=nomeClasseRR%>.getClasse().getDados().toJsonRosenaString();
                                                var width = 150;
                                                var height = 250;
                                                var left = 99;
                                                var top = 99;
                                                window.open(pagina,'_UPLOAD');
                                            }catch(ex){
                                                alert(ex.message);
                                            }
                                        }
                                    </script>
                                    <%=atributoR.isObrigatorio() && !atributoR.isSomenteLeitura() ? "<font color=\"red\" >"+atributoR.getNomeUsual().replace("_"," ")+"</font>": atributoR.getNomeUsual().replace("_"," ")%>:<br>
                                    <input id="<%=atributoR.getNome()%><%=nomeClasseRR%>" name="<%=atributoR.getNome()%><%=nomeClasseRR%>" onchange="obj<%=nomeClasseRR%>.setString('<%=atributoR.getNome()%>',document.getElementById('<%=atributoR.getNome()%><%=nomeClasseRR%>').value);" type="file" disabled="disabled"  class="<%=atributoR.isSomenteLeitura() ? "Input04" : "Input04" %>" value="<%=objPersistenteRR.getString(atributoR.getNome())%>">
                                </fieldset>
                            </td>   
                        <%}else{%>
                             <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributoR.getTamanhoLargura(),"0")%>">
                                <%if(atributoR.getTamanhoAltura()>1){%>
                                    <%=atributoR.isObrigatorio() && !atributoR.isSomenteLeitura() ? "<font color=\"red\" >"+atributoR.getNomeUsual().replace("_"," ")+"</font>": atributoR.getNomeUsual().replace("_"," ")%>:<br>
                                    <textarea id="<%=atributoR.getNome()%>" rows="<%=br.com.rosena.util.Util.formatarNumero(atributoR.getTamanhoAltura()/2,"0")%>" name="<%=atributoR.getNome()%><%=nomeClasseRR%>" onchange="obj<%=nomeClasseRR%>.setString('<%=atributoR.getNome()%>',document.getElementById('<%=atributoR.getNome()%><%=nomeClasseRR%>').value);"  <%=atributoR.isSomenteLeitura() ? "disabled=\"disable\"" : ""%>  class="<%=atributoR.isSomenteLeitura() ? "Input04" : "Input01" %>" ><%=objPersistenteRR.getString(atributoR.getNome())%></textarea>
                                <%}else{%>
                                    <%=atributoR.isObrigatorio() && !atributoR.isSomenteLeitura() ? "<font color=\"red\" >"+atributoR.getNomeUsual().replace("_"," ")+"</font>": atributoR.getNomeUsual().replace("_"," ")%>:<br>
                                    <input id="<%=atributoR.getNome()%>" name="<%=atributoR.getNome()%><%=nomeClasseRR%>" onchange="obj<%=nomeClasseRR%>.setString('<%=atributoR.getNome()%>',document.getElementById('<%=atributoR.getNome()%><%=nomeClasseRR%>').value);" type="text" <%=atributoR.isSomenteLeitura() ? "disabled=\"disable\"" : ""%>  class="<%=atributoR.isSomenteLeitura() ? "Input04" : "Input01" %>" value="<%=objPersistenteRR.getString(atributoR.getNome())%>">
                                <%}%>
                            </td>
                        <%}%>
                <%}else if(tipoAtributoRR.equals(Atributo.Boolean)){%>
                        <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributoR.getTamanhoLargura(),"0")%>">
                            <%=atributoR.isObrigatorio() && !atributoR.isSomenteLeitura() ? "<font color=\"red\" >"+atributoR.getNomeUsual().replace("_"," ")+"</font>": atributoR.getNomeUsual().replace("_"," ")%>:<br>
                            <input id="<%=atributoR.getNome()%><%=nomeClasseRR%>"  name="<%=atributoR.getNome()%><%=nomeClasseRR%>" onchange="obj<%=nomeClasseRR%>.setBoolean('<%=atributoR.getNome()%>',document.getElementById('<%=atributoR.getNome()%><%=nomeClasseRR%>').checked);"  type="checkbox"  <%=atributoR.isSomenteLeitura() ? "disabled=\"disable\"" : ""%> checked="<%=objPersistenteRR.getBoolean(atributoR.getNome())?"checked":""%>">
                        </td>
                <%}else if(tipoAtributoRR.equals(Atributo.Double)){%>
                        <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributoR.getTamanhoLargura(),"0")%>">
                            <%=atributo.isObrigatorio() && !atributo.isSomenteLeitura() ? "<font color=\"red\" >"+atributo.getNomeUsual().replace("_"," ")+"</font>": atributo.getNomeUsual().replace("_"," ")%>:<br>
                            <input id="<%=atributo.getNome()%><%=nomeClasseRR%>" name="<%=atributo.getNome()%><%=nomeClasseRR%>" onchange="obj<%=nomeClasseRR%>.setDouble('<%=atributo.getNome()%>',document.getElementById('<%=atributo.getNome()%><%=nomeClasseRR%>').value);"  type="text" <%=atributo.isSomenteLeitura() ? "disabled=\"disable\"" : ""%> onblur="formatarNumero(this,'0.00')"  class="<%=atributo.isSomenteLeitura() ? "Input04" : "Input01" %>" value="<%=br.com.rosena.util.Util.formatarNumero(objPersistenteRR.getDouble(atributo.getNome()),"0.00")%>">
                        </td>
                <%}else if(tipoAtributoRR.equals(Atributo.Integer)){%>
                        <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributo.getTamanhoLargura(),"0")%>">
                            <%=atributo.isObrigatorio() && !atributo.isSomenteLeitura() ? "<font color=\"red\" >"+atributo.getNomeUsual().replace("_"," ")+"</font>": atributo.getNomeUsual().replace("_"," ")%>:<br>
                            <input id="<%=atributo.getNome()%><%=nomeClasseRR%>" name="<%=atributo.getNome()%><%=nomeClasseRR%>" onchange="obj<%=nomeClasseRR%>.setInt('<%=atributo.getNome()%>',document.getElementById('<%=atributo.getNome()%><%=nomeClasseRR%>').value);" type="text" <%=atributo.isSomenteLeitura() ? "disabled=\"disable\"" : ""%> onblur="formatarNumero(this,'0')" class="<%=atributo.isSomenteLeitura() ? "Input04" : "Input01" %>" value="<%=objPersistenteRR.getInt(atributo.getNome())%>">
                        </td>
                <%}else if(tipoAtributoRR.equals(Atributo.Date)){%>
                        <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributo.getTamanhoLargura(),"0")%>">
                            <%=atributo.isObrigatorio() && !atributo.isSomenteLeitura() ? "<font color=\"red\" >"+atributo.getNomeUsual().replace("_"," ")+"</font>": atributo.getNomeUsual().replace("_"," ")%>:<br>
                            <input id="<%=atributo.getNome()%><%=nomeClasseRR%>" name="<%=atributo.getNome()%><%=nomeClasseRR%>" onchange="obj<%=nomeClasseRR%>.setDate('<%=atributo.getNome()%>',document.getElementById('<%=atributo.getNome()%><%=nomeClasseRR%>').value);" type="text" <%=atributo.isSomenteLeitura() ? "disabled=\"disable\"" : ""%>  class="<%=atributo.isSomenteLeitura() ? "Input04" : "Input01" %>" value="<%=br.com.rosena.util.Util.formatarData(objPersistenteRR.getDate(atributo.getNome()),"dd/MM/yyyy")%>">
                        </td>
                 <%}else if(tipoAtributoRR.equals(Atributo.Time)){%>
                        <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributo.getTamanhoLargura(),"0")%>">
                            <%=atributo.isObrigatorio() && !atributo.isSomenteLeitura() ? "<font color=\"red\" >"+atributo.getNomeUsual().replace("_"," ")+"</font>": atributo.getNomeUsual().replace("_"," ")%>:<br>
                            <input id="<%=atributo.getNome()%><%=nomeClasseRR%>" name="<%=atributo.getNome()%><%=nomeClasseRR%>" onchange="obj<%=nomeClasseRR%>.setTime('<%=atributo.getNome()%>',document.getElementById('<%=atributo.getNome()%><%=nomeClasseRR%>').value);" type="text" <%=atributo.isSomenteLeitura() ? "disabled=\"disable\"" : ""%>  class="<%=atributo.isSomenteLeitura() ? "Input04" : "Input01" %>" value="<%=br.com.rosena.util.Util.formatarData(objPersistenteRR.getTime(atributo.getNome()),"HH:mm")%>">
                        </td>
                 <%}else if(tipoAtributoRR.equals(Atributo.TimeStamp)){%>
                        <td  colspan="<%= br.com.rosena.util.Util.formatarNumero(atributo.getTamanhoLargura(),"0")%>">
                            <%=atributo.isObrigatorio() && !atributo.isSomenteLeitura() ? "<font color=\"red\" >"+atributo.getNomeUsual().replace("_"," ")+"</font>": atributo.getNomeUsual().replace("_"," ")%>:<br>
                            <input id="<%=atributo.getNome()%><%=nomeClasseRR%>" name="<%=atributo.getNome()%><%=nomeClasseRR%>" onchange="obj<%=nomeClasseRR%>.setTime('<%=atributo.getNome()%>',document.getElementById('<%=atributo.getNome()%><%=nomeClasseRR%>').value);" type="text" <%=atributo.isSomenteLeitura() ? "disabled=\"disable\"" : ""%>  class="<%=atributo.isSomenteLeitura() ? "Input04" : "Input01" %>" value="<%=br.com.rosena.util.Util.formatarData(objPersistenteRR.getTime(atributo.getNome()),"dd/MM/yyyy HH:mm:ss")%>">
                        </td>
                 <%}%>
                <%if(colspan >= 8){%>
                    </tr>
                    <%colspan=0;
                      entrar = true;%>
                <%}%>
            <%}%>
        <tr>
            <td><br></td>
        </tr>
        <tr align="center">
            <td colspan="8">
                <div class="divBotao" >
                    <table  class="Tabela01" cellpadding="0" cellspacing="0" align="center" >
                        <tr align="center">
                            <td>
                                <%for (int ii = 0; ii < lstMetodoRR.length; ii++) {
                                    Metodo metodo = lstMetodoRR[ii];%>
                                    <%if(metodo.getOrdem() != -1){%>
                                        <script>

                                             function <%=metodo.getNome()%>_<%=nomeClasseRR%>(){
                                                obj<%=nomeClasseRR%>.setPersistente('<%=lstAtributo[i].getValorPadraoTela()%>',obj<%=nomeClasse%>.getString("id") );//inversa
                                                obj<%=nomeClasseRR%>.invocarVoid('<%=metodo.getNome()%>','<%=nomeClasseRR%>');
                                                xmlhttpPostLocalizar('<%=nomeClasseRR%>',obj<%=nomeClasse%>.getString("id"),'<%=lstAtributo[i].getValorPadraoTela()%>');
                                            }
                                        </script>
                                        <input class="Botao01" onclick="<%=metodo.getNome()%>_<%=nomeClasseRR%>();" type="button" value="<%=metodo.getNomeUsual()%>">
                                    <%}%>
                                <%}%>
                                 <input class="Botao01" onclick="obj<%=nomeClasseRR%> = limparCampos<%=nomeClasseRR%>('<%=nomeClasseRR%>',obj<%=nomeClasseRR%>,'frm');" type="button" value="Limpar">
                                <script>
                                    function limparCampos<%=nomeClasseRR%>(nomeClasse,objPersistente,frm){

                                        objPersistente = new Persistente(nomeClasse);
                                        var json = objPersistente.getClasse().getDados().toJsonRosenaString().replace("{","").replace("}","");
                                        var jsonArr = json.replace("{").replace("}").split(",");

                                        for(var i=0;i<jsonArr.length;i++){
                                            var jsonArrZero = jsonArr[i].split(":");
                                            jsonArrZero[0] = jsonArrZero[0].replace("\"","") ;
                                            jsonArrZero[0] = jsonArrZero[0].replace("\"","") ;

                                            if(jsonArrZero[0]!="id"){
                                                var doc = document.getElementsByName(jsonArrZero[0]+nomeClasse);
                                                for(var j=0;j<doc.length;j++){
                                                    if(doc[j].name != undefined){
                                                        switch (doc[j].type){
                                                            case 'text'  : doc[j].value   = ''; break;
                                                            case 'radio' : doc[j].checked    = ''; break;
                                                            case 'checkbox' : doc[j].checked    = ''; break;
                                                            case 'select-one' : doc[j].value    = '0'; break;
                                                            case 'textarea' : doc[j].value    = ''; break;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        return objPersistente;
                                    }
                                </script>
                            </td>
                        </tr>
                    </table>
                </div>
            </td>
        </tr>
    </table>
    <div id="localizar<%=nomeClasseRR%>" style="width:100%;height:50%;overflow:auto;"></div>
</fieldset>


